package com.jhgsys.internal.peccancy.service.impl;

import com.jhgsys.internal.peccancy.entity.PeccancyClauseRule;
import com.jhgsys.internal.peccancy.mapper.PeccancyClauseRuleMapper;
import com.jhgsys.internal.peccancy.service.IPeccancyClauseRuleService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 违章条款奖惩规则 Service实现
 *
 * @author admin
 * @date 2024-08-08 13:20:28
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class PeccancyClauseRuleServiceImpl extends ServiceImpl<PeccancyClauseRuleMapper, PeccancyClauseRule> implements IPeccancyClauseRuleService {

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-08-08 13:20:28
     * @param request QueryRequest
     * @param peccancyClauseRule peccancyClauseRule
     * @return IPage<PeccancyClauseRule>
    */
    @Override
    public IPage<PeccancyClauseRule> findPeccancyClauseRulePage(QueryRequest request, PeccancyClauseRule peccancyClauseRule) {
        LambdaQueryWrapper<PeccancyClauseRule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PeccancyClauseRule::getDeletedFlg, 0);
        if(StringUtils.isNotBlank(peccancyClauseRule.getClauseId())){
            queryWrapper.eq(PeccancyClauseRule::getClauseId, peccancyClauseRule.getClauseId());
        }
        if(StringUtils.isNotBlank(peccancyClauseRule.getClauseNo())){
            queryWrapper.like(PeccancyClauseRule::getClauseNo, peccancyClauseRule.getClauseNo());
        }
        if(StringUtils.isNotBlank(peccancyClauseRule.getTenantId())){
            queryWrapper.eq(PeccancyClauseRule::getTenantId, peccancyClauseRule.getTenantId());
        }
        if(StringUtils.isNotBlank(peccancyClauseRule.getClauseContent())){
            queryWrapper.like(PeccancyClauseRule::getClauseContent, peccancyClauseRule.getClauseContent());
        }
        if(StringUtils.isNotBlank(peccancyClauseRule.getStatus())){
            queryWrapper.eq(PeccancyClauseRule::getStatus, peccancyClauseRule.getStatus());
        }
        if(StringUtils.isNotBlank(peccancyClauseRule.getDisposeForm())){
            queryWrapper.eq(PeccancyClauseRule::getDisposeForm, peccancyClauseRule.getDisposeForm());
        }
        queryWrapper.orderByAsc(PeccancyClauseRule::getClauseLevel);
        Page<PeccancyClauseRule> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-08-08 13:20:28
     * @param peccancyClauseRule peccancyClauseRule
     * @return List<PeccancyClauseRule>
     */
    @Override
    public List<PeccancyClauseRule> findPeccancyClauseRuleList(PeccancyClauseRule peccancyClauseRule) {
	    LambdaQueryWrapper<PeccancyClauseRule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PeccancyClauseRule::getDeletedFlg, 0);
        if(StringUtils.isNotBlank(peccancyClauseRule.getTenantId())){
            queryWrapper.eq(PeccancyClauseRule::getTenantId, peccancyClauseRule.getTenantId());
        }
        if(StringUtils.isNotBlank(peccancyClauseRule.getClauseId())){
            queryWrapper.eq(PeccancyClauseRule::getClauseId, peccancyClauseRule.getClauseId());
        }
        if(StringUtils.isNotBlank(peccancyClauseRule.getStatus())){
            queryWrapper.eq(PeccancyClauseRule::getStatus, peccancyClauseRule.getStatus());
        }
        if(StringUtils.isNotBlank(peccancyClauseRule.getClauseNo())){
            queryWrapper.eq(PeccancyClauseRule::getClauseNo, peccancyClauseRule.getClauseNo());
        }
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author admin
    * @date 2024-08-08 13:20:28
    * @param id
    * @return PeccancyClauseRule
    */
    @Override
    public PeccancyClauseRule findPeccancyClauseRuleById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author admin
     * @date 2024-08-08 13:20:28
     * @param peccancyClauseRule peccancyClauseRule
     */
    @Override
    @Transactional
    public void createPeccancyClauseRule(PeccancyClauseRule peccancyClauseRule) {
        peccancyClauseRule.setCreatedTime(new Date());
        this.save(peccancyClauseRule);
    }

    /**
     * 修改
     * @author admin
     * @date 2024-08-08 13:20:28
     * @param peccancyClauseRule peccancyClauseRule
     */
    @Override
    @Transactional
    public void updatePeccancyClauseRule(PeccancyClauseRule peccancyClauseRule) {
        peccancyClauseRule.setUpdatedTime(new Date());
        this.saveOrUpdate(peccancyClauseRule);
    }

    /**
     * 逻辑删除
     * @author admin
     * @date 2024-08-08 13:20:28
     * @param peccancyClauseRule peccancyClauseRule
     * @param peccancyClauseRuleIds peccancyClauseRuleIds
     */
    @Override
    @Transactional
    public void deletePeccancyClauseRule(PeccancyClauseRule peccancyClauseRule,String peccancyClauseRuleIds) {
        List<String> list = Arrays.asList(peccancyClauseRuleIds.split(StringPool.COMMA));
        this.baseMapper.update(peccancyClauseRule,new LambdaQueryWrapper<PeccancyClauseRule>().in(PeccancyClauseRule::getClauseRuleId,list));
	}

    /**
    * 物理删除
    * @author admin
    * @date 2024-08-08 13:20:28
    * @param peccancyClauseRuleIds peccancyClauseRuleIds
    */
    @Override
    @Transactional
    public void physicallyDeletePeccancyClauseRule(String peccancyClauseRuleIds) {
        List<String> list = Arrays.asList(peccancyClauseRuleIds.split(StringPool.COMMA));
        LambdaQueryWrapper<PeccancyClauseRule> wapper = new LambdaQueryWrapper<>();
        wapper.in(PeccancyClauseRule::getClauseRuleId,list);
        this.remove(wapper);
    }
}
